package translate;

import it.unimi.dsi.fastutil.bytes.ByteArrayList;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import net.olivo.lc4j.LanguageCategorization;

import parser.ParserAnnotation;
import asa.PostBucket;
import forum.ForumPost;

public class RemoveTranslate extends Translate {
	private LanguageCategorization lc;

	public RemoveTranslate(PostBucket inBucket, PostBucket translateBucket) {
		super("RemoveTranslate", inBucket, translateBucket);
		lc = new LanguageCategorization();
		lc.setLanguageModelsDir("models/language");
	}

	@Override
	public List<ForumPost> translate(List<ForumPost> posts) {
		List<ForumPost> translatedPosts = new ArrayList<ForumPost>();
		for (ForumPost post : posts) {
			if (isEnglish(post))
				translatedPosts.add(post);
		}
		return translatedPosts;
	}

	private boolean isEnglish(ForumPost post) {
		List<String> languages = getLanguages(post);
		for (String language : languages) {
			if (language.startsWith("english") || language.startsWith("scots")
					|| language.startsWith("welsh")) {
				return true;
			}
		}
		return false;
	}

	private List<String> getLanguages(ForumPost forumPost) {
		ByteArrayList bal = new ByteArrayList();

		String title = forumPost.getTitle();
		title = title.concat(" ");

		for (Byte s : title.getBytes()) {
			bal.add(s);
		}

		ParserAnnotation pa = forumPost.getParserAnnotation();
		String doc = pa.getDocument();
		for (Byte s : doc.getBytes()) {
			bal.add(s);
		}
		PrintStream error = System.err;

		System.setErr(new PrintStream(new OutputStream() {
			public void write(int b) {
			}
		}));

		List<String> language = lc.findLanguage(bal);
		System.setErr(error);
		return language;
	}
}
